#include "adLogic.h"
#include "adDao.h"
#include "glib.h"
#include "gio/gio.h"
#include <stdio.h>
#include "curlRequest.h"
#define RES_PATH "/home/syc/ronghuanet_ads/resources/"

Ad* parseUpDownSplitScr(cJSON *adJson)
{
  Ad * result = calloc(1,sizeof(Ad));
  if (cJSON_IsObject(adJson))
  {
      //自己的一些属性解析
      cJSON *idJson = cJSON_GetObjectItemCaseSensitive(adJson,"id");
      int id = idJson->valueint;
      cJSON *templateIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"template_id");
      int templateId = templateIdJson->valueint;
      cJSON *customerIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"customer_id");
      int customerId = customerIdJson->valueint;
      cJSON *startTimeJson = cJSON_GetObjectItemCaseSensitive(adJson,"start_time");
      char *startTime = startTimeJson->valuestring;
      cJSON *endTimeJson = cJSON_GetObjectItemCaseSensitive(adJson,"end_time");
      char *endTime = endTimeJson->valuestring;
      result->id  = id;
      result->template_id  = templateId;
      result->customer_id  = customerId;
      result->start_time  = startTime;
      result->end_time  = endTime;

      //广告资源解析
      AdResource *upAr = calloc(1,sizeof(AdResource));
      upAr->ad_id = id;
      upAr->index = 0;
      cJSON *typeIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_type_id");
      int typeId = typeIdJson->valueint;
      upAr->type_id = typeId;
      cJSON *urlJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_url");
      char *url = urlJson->valuestring;
      upAr->path = url;
      cJSON *signJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_sign");
      char *sign = signJson->valuestring;
       upAr->sign = sign;
    
      result->adResources[0] =upAr;

      AdResource *buttomAr = calloc(1,sizeof(AdResource));
      buttomAr->ad_id = id;
      buttomAr->index = 0;
      cJSON *typeIdJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_type_id");
      int typeId1 = typeIdJson1->valueint;
      buttomAr->type_id = typeId1;
      cJSON *urlJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_url");
      char *url1 = urlJson1->valuestring;
      buttomAr->path = url1;
      cJSON *signJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_sign");
      char *sign1 = signJson1->valuestring;
       buttomAr->sign = sign1;
    
      result->adResources[1] =buttomAr;
      result->adResources[2]=NULL;
  }
  return result;
}

//获得资源路径
char*  getResPath(AdResource *ar)
{
    int adId = ar->ad_id;
    gchar* fileName = g_path_get_basename(ar->path);
    //准备一个格式文件，格式一下字符串
    char* formatStr="%s/%d_%s";//资源路径/id_name
    return g_strdup_printf(formatStr,RES_PATH,adId,fileName);
}

//下载广告并且生成本地广告列表到SQLite
int downloadAdsAndGenLocalAdListToSqlite(Ad* ads[],int size)
{
    //写个状态标识用来表示现在是什么样的状态 1代表更新
    int stateId = 0;
    for (int i = 0; i < size; i++)
    {
        Ad *ad = ads[i];
        int adId = ad->id;
        Ad *adInDb = getAd(adId);//先查一下存在不
        if (adInDb==NULL)//如果不存在，就要把它保存起来
        {
            stateId = 1;
            saveAd(ad);
        
            for (int  j = 0; j < 3; j++)
            {
                AdResource* adr = ad->adResources[j];
                if (adr!=NULL)//第三个是NULL
                {
                    char* localAdrPath = getResPath(adr);
                    gboolean exits = g_file_test(localAdrPath,G_FILE_TEST_EXISTS);//判断该路径对应的文件是否存在
                    if (!exits)//如果不存在
                    {
                        GFile * file = g_file_new_for_path(localAdrPath);
                        if (!g_file_create(file,0,NULL,NULL))
                        {
                            printf("create fiel fail\n");
                        }
                        //下载
                        downloadRes(adr->path,localAdrPath);
                        //保存
                        saveAr(adr);
                    }
                    
                }
            }
        }
        else//如果存在，则要更新一波
        {
            //直接更新
            updateAd(ad);
            for (int  j = 0; j < 3; j++)
            {
                AdResource* adr = ad->adResources[j];
                if (adr!=NULL)//第三个是NULL
                {
                    char* localAdrPath = getResPath(adr);
                    char* cn = adr->sign;//远程的加密sign
                    char* localCn = "jjjjj";//获取本地文件的加密串  这个文件进行md5计算
                    if (!g_strcmp0(cn,localCn))
                    {
                        gboolean exits = g_file_test(localAdrPath,G_FILE_TEST_EXISTS);//判断该路径对应的文件是否存在
                    if (!exits)//如果不存在
                    {
                        GFile * file = g_file_new_for_path(localAdrPath);
                        if (!g_file_create(file,0,NULL,NULL))
                        {
                            printf("create fiel fail\n");
                        }
                        //下载
                        downloadRes(adr->path,localAdrPath);
                        //保存
                        saveAr(adr);
                    }
                    }
                }
            }
        }
        
        
    }
    
    
}

void handleAdList(cJSON* adListArry)
{
    //1.判断是不是数组
    if (!cJSON_IsArray(adListArry))
    {
        return ;
    }
    //准备广告机的数组，存放解析后得到数据
    int size = cJSON_GetArraySize(adListArry);
    Ad *ads[size];
    for (int i = 0; i < size; i++)
    {
        cJSON *adJsonStr = cJSON_GetArrayItem(adListArry,i);
        int templateId = cJSON_GetObjectItemCaseSensitive(adJsonStr,"template_id")->valueint;
        switch (templateId)
        {
        case 6:
        //上下分屏的结构
            Ad *ad = parseUpDownSplitScr(adJsonStr);
            ads[i]=ad;
            break;
        case 5:
            break;
        default:
            g_print("template_id非法\n");
            break;
        }
    }

    //3.准备把数据存储到sqlite3，并且进行资源同步
    int stateId= downloadAdsAndGenLocalAdListToSqlite(ads,size);//返回值是1 有资源发生变化

    //4.把new资源释放


    //5.如果资源/广告列表有更新变化，qt进程重新加载内容
    if (stateId==1)
    {
        //进程之间的通讯
    }
}
